import gtk, os, sys
import frame, track, one_timeline
import text
class timeline(gtk.ScrolledWindow):
    def __init__(self, project_folder, action):
        gtk.ScrolledWindow.__init__(self)
        self.set_size_request(-1, 200)
        self.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        VBox=gtk.VBox(spacing=5)
        VBox.set_size_request(-1, 150)
        self.frame_timeline=one_timeline.timeline("Photos")
        self.frame_timeline.set_size_request(-1, 50)
        self.text_timeline=one_timeline.timeline("Text")
        self.text_timeline.set_size_request(-1, 50)
        self.track_timeline=one_timeline.timeline("Tracks")
        self.track_timeline.set_size_request(-1, 50)
        VBox.add(self.frame_timeline)
        VBox.add(self.text_timeline)
        VBox.add(self.track_timeline)
        self.action=action
        
        self.add_with_viewport(VBox)
        
        self.list_frames=[]
        self.list_text=[]
        self.list_tracks=[]
        
        self.project_folder=project_folder
        self.read_project_file()
        
        self.selected=None
    
    def repaint(self):
        self.frame_timeline.clear()
        self.text_timeline.clear()
        self.track_timeline.clear()
        
        for frame in self.list_frames:
            width=frame.len * 10 -1
            frame.set_width(width)
            frame.show_all()
            frame.connect("button-press-event", self.clicked)
            self.frame_timeline.add_frame(frame)
        
        for txt in self.list_text:
            width=txt.len * 10 -1
            txt.set_width(width)
            txt.show_all()
            txt.connect("button-press-event", self.clicked)
            self.text_timeline.add_txt(txt)
        
        
        for track in self.list_tracks:
            width=track.len * 10
            track.set_width(width)
            self.track_timeline.add(track)
        
        self.show_all()
        self.rewrite_config()
    
    def add_frame(self, photo):
        new_frame=frame.frame(photo, self.project_folder)
        self.list_frames.append(new_frame)
        self.save()
    
    def add_txt(self, txt=""):
        new_text=text.text(txt, self.project_folder)
        self.list_text.append(new_text)
        self.save()
    
    def add_track(self, new_track):
        self.list_tracks.append(new_track)
        self.save()
    
    def remove_all_frames(self):
        self.list_frames=[]
        os.system("rm -R " + self.project_folder + "photos/")
        os.system("rm -R " + self.project_folder + "thumb/")
        self.save()
    
    def remove_selected(self):
        if (self.selected!=None):
            if isinstance(self.selected, frame.frame):
                os.system("rm " + self.selected.photo)
                os.system("rm " + self.selected.thumbnail)
                self.list_frames.remove(self.selected)
                self.selected=None
            elif isinstance(self.selected, track.track):
                self.list_tracks.remove(self.selected)
                self.selected=None
            elif isinstance(self.selected, text.text):
                os.system("rm " + self.selected.photo)
                os.system("rm " + self.selected.thumbnail)
                self.list_text.remove(self.selected)
                self.selected=None
            self.save()
            self.action(None)
        pass
    
    def get_selected(self):
        pass
    
    def get_frame_list(self):
        return self.list_frames
    
    def get_text_list(self):
        return self.list_text
    
    def get_track_list(self):
        return self.list_tracks
    
    def read_project_file(self):
        photos=[]
        texts=[]
        try:
            file=open(self.project_folder+"project_photos", "r")
            photos=file.readlines()
        except: print "error", sys.exc_info()[1]
        try:
            file2=open(self.project_folder+"project_text", "r")
            texts=file2.readlines()
        except: print "error", sys.exc_info()[1]
        
        for el in photos:
            el=el.replace("\n", "")
            if os.path.exists(el.split("#")[0]):
                self.add_frame(el)
            else: print el, "seem to be moved or removed."
        
        for el in texts:
            el=el.replace("\n", "")
            print el
            self.add_txt(el)
        self.repaint()
        
    def save(self):
        self.rewrite_config()
        self.repaint()
    
    def rewrite_config(self):
        file=open(self.project_folder+"project_photos", "w")
        for el in self.get_frame_list():
            file.write(el.to_string()+"\n")
        file.close()
        file=open(self.project_folder+"project_text", "w")
        for el in self.get_text_list():
            file.write(el.to_string()+"\n")
        file.close()
    
    def clicked(self, obj, event):
        #print obj
        if event.button==1 and self.selected!=obj:
            self.selected=obj
            for frame in self.list_frames:
                frame.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("#BBBBBB"))
            obj.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("#65BBFF"))
            self.action(obj)
    
    def as_this(self, obj):
        if isinstance(obj, frame.frame):
            for edit_frame in self.list_frames:
                edit_frame.trans_effect=obj.trans_effect
                edit_frame.len=obj.len
                edit_frame.len_effect=obj.len_effect
            self.save()
        elif isinstance(obj, track):
            pass
        